<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>csv_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_format_csv_test.js.html">csv_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.labs.format.csvTest&#39;);
<a name="line16"></a>
<a name="line17"></a>goog.require(&#39;goog.labs.format.csv&#39;);
<a name="line18"></a>goog.require(&#39;goog.labs.format.csv.ParseError&#39;);
<a name="line19"></a>goog.require(&#39;goog.object&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.setTestOnly(&#39;goog.labs.format.csvTest&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>function testGoldenPath() {
<a name="line27"></a>  assertObjectEquals(
<a name="line28"></a>      [[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;], [&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]],
<a name="line29"></a>      goog.labs.format.csv.parse(&#39;a,b,c\nd,e,f\ng,h,i\n&#39;));
<a name="line30"></a>  assertObjectEquals(
<a name="line31"></a>      [[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;], [&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]],
<a name="line32"></a>      goog.labs.format.csv.parse(&#39;a,b,c\r\nd,e,f\r\ng,h,i\r\n&#39;));
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>function testNoCrlfAtEnd() {
<a name="line36"></a>  assertObjectEquals(
<a name="line37"></a>      [[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;], [&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]],
<a name="line38"></a>      goog.labs.format.csv.parse(&#39;a,b,c\nd,e,f\ng,h,i&#39;));
<a name="line39"></a>}
<a name="line40"></a>
<a name="line41"></a>function testQuotes() {
<a name="line42"></a>  assertObjectEquals(
<a name="line43"></a>      [[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;], [&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]],
<a name="line44"></a>      goog.labs.format.csv.parse(&#39;a,&quot;b&quot;,c\n&quot;d&quot;,&quot;e&quot;,&quot;f&quot;\ng,h,&quot;i&quot;&#39;));
<a name="line45"></a>  assertObjectEquals(
<a name="line46"></a>      [[&#39;a&#39;, &#39;b, as in boy&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;]],
<a name="line47"></a>      goog.labs.format.csv.parse(&#39;a,&quot;b, as in boy&quot;,c\n&quot;d&quot;,&quot;e&quot;,&quot;f&quot;\n&#39;));
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>function testEmbeddedCrlfs() {
<a name="line51"></a>  assertObjectEquals(
<a name="line52"></a>      [[&#39;a&#39;, &#39;b\nball&#39;, &#39;c&#39;], [&#39;d\nd&#39;, &#39;e&#39;, &#39;f&#39;], [&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]],
<a name="line53"></a>      goog.labs.format.csv.parse(&#39;a,&quot;b\nball&quot;,c\n&quot;d\nd&quot;,&quot;e&quot;,&quot;f&quot;\ng,h,&quot;i&quot;\n&#39;));
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function testEmbeddedQuotes() {
<a name="line57"></a>  assertObjectEquals(
<a name="line58"></a>      [[&#39;a&#39;, &#39;&quot;b&quot;&#39;, &#39;Jonathan &quot;Smokey&quot; Feinberg&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;]],
<a name="line59"></a>      goog.labs.format.csv.parse(
<a name="line60"></a>          &#39;a,&quot;&quot;&quot;b&quot;&quot;&quot;,&quot;Jonathan &quot;&quot;Smokey&quot;&quot; Feinberg&quot;\nd,e,f\r\n&#39;));
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>function testUnclosedQuote() {
<a name="line64"></a>  var e = assertThrows(function() {
<a name="line65"></a>    goog.labs.format.csv.parse(&#39;a,&quot;b,c\nd,e,f&#39;);
<a name="line66"></a>  });
<a name="line67"></a>
<a name="line68"></a>  assertTrue(e instanceof goog.labs.format.csv.ParseError);
<a name="line69"></a>  assertEquals(2, e.position.line);
<a name="line70"></a>  assertEquals(5, e.position.column);
<a name="line71"></a>  assertEquals(
<a name="line72"></a>      &#39;Unexpected end of text after open quote at line 2 column 5\n&#39; +
<a name="line73"></a>      &#39;d,e,f\n&#39; +
<a name="line74"></a>      &#39;    ^&#39;,
<a name="line75"></a>      e.message);
<a name="line76"></a>}
<a name="line77"></a>
<a name="line78"></a>function testQuotesInUnquotedField() {
<a name="line79"></a>  var e = assertThrows(function() {
<a name="line80"></a>    goog.labs.format.csv.parse(&#39;a,b &quot;and&quot; b,c\nd,e,f&#39;);
<a name="line81"></a>  });
<a name="line82"></a>
<a name="line83"></a>  assertTrue(e instanceof goog.labs.format.csv.ParseError);
<a name="line84"></a>
<a name="line85"></a>  assertEquals(1, e.position.line);
<a name="line86"></a>  assertEquals(5, e.position.column);
<a name="line87"></a>
<a name="line88"></a>  assertEquals(
<a name="line89"></a>      &#39;Unexpected quote mark at line 1 column 5\n&#39; +
<a name="line90"></a>      &#39;a,b &quot;and&quot; b,c\n&#39; +
<a name="line91"></a>      &#39;    ^&#39;,
<a name="line92"></a>      e.message);
<a name="line93"></a>}
<a name="line94"></a>
<a name="line95"></a>function testGarbageOutsideQuotes() {
<a name="line96"></a>  var e = assertThrows(function() {
<a name="line97"></a>    goog.labs.format.csv.parse(&#39;a,&quot;b&quot;,c\nd,&quot;e&quot;oops,f&#39;);
<a name="line98"></a>  });
<a name="line99"></a>
<a name="line100"></a>  assertTrue(e instanceof goog.labs.format.csv.ParseError);
<a name="line101"></a>  assertEquals(2, e.position.line);
<a name="line102"></a>  assertEquals(6, e.position.column);
<a name="line103"></a>  assertEquals(
<a name="line104"></a>      &#39;Unexpected character &quot;o&quot; after quote mark at line 2 column 6\n&#39; +
<a name="line105"></a>      &#39;d,&quot;e&quot;oops,f\n&#39; +
<a name="line106"></a>      &#39;     ^&#39;,
<a name="line107"></a>      e.message);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testEmptyRecords() {
<a name="line111"></a>  assertObjectEquals(
<a name="line112"></a>      [[&#39;a&#39;, &#39;&#39;, &#39;c&#39;], [&#39;d&#39;, &#39;e&#39;, &#39;&#39;], [&#39;&#39;, &#39;&#39;, &#39;&#39;]],
<a name="line113"></a>      goog.labs.format.csv.parse(&#39;a,,c\r\nd,e,\n,,&#39;));
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>function testIgnoringErrors() {
<a name="line117"></a>  // The results of these tests are not defined by the RFC. They
<a name="line118"></a>  // generally strive to be &quot;reasonable&quot; while keeping the code simple.
<a name="line119"></a>
<a name="line120"></a>  // Quotes inside field
<a name="line121"></a>  assertObjectEquals(
<a name="line122"></a>      [[&#39;Hello &quot;World&quot;!&#39;, &#39;b&#39;], [&#39;c&#39;, &#39;d&#39;]], goog.labs.format.csv.parse(
<a name="line123"></a>          &#39;Hello &quot;World&quot;!,b\nc,d&#39;, true));
<a name="line124"></a>
<a name="line125"></a>  // Missing closing quote
<a name="line126"></a>  assertObjectEquals(
<a name="line127"></a>      [[&#39;Hello&#39;, &#39;World!&#39;]], goog.labs.format.csv.parse(
<a name="line128"></a>          &#39;Hello,&quot;World!&#39;, true));
<a name="line129"></a>
<a name="line130"></a>  // Broken use of quotes in quoted field
<a name="line131"></a>  assertObjectEquals(
<a name="line132"></a>      [[&#39;a&#39;, &#39;&quot;Hello&quot;World!&quot;&#39;]], goog.labs.format.csv.parse(
<a name="line133"></a>          &#39;a,&quot;Hello&quot;World!&quot;&#39;, true));
<a name="line134"></a>
<a name="line135"></a>  // All of the above. A real mess.
<a name="line136"></a>  assertObjectEquals(
<a name="line137"></a>      [[&#39;This&quot; is&#39;, &#39;&quot;very\n\tvery&quot;broken&quot;&#39;, &#39; indeed!&#39;]],
<a name="line138"></a>      goog.labs.format.csv.parse(
<a name="line139"></a>          &#39;This&quot; is,&quot;very\n\tvery&quot;broken&quot;,&quot; indeed!&#39;, true));
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>function testIgnoringErrorsTrailingTabs() {
<a name="line143"></a>  assertObjectEquals(
<a name="line144"></a>      [[&#39;&quot;a\tb&quot;\t&#39;], [&#39;c,d&#39;]], goog.labs.format.csv.parse(
<a name="line145"></a>          &#39;&quot;a\tb&quot;\t\n&quot;c,d&quot;&#39;, true));
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>function testFindLineInfo() {
<a name="line149"></a>  var testString = &#39;abc\ndef\rghi&#39;;
<a name="line150"></a>  var info = goog.labs.format.csv.ParseError.findLineInfo_(testString, 4);
<a name="line151"></a>
<a name="line152"></a>  assertEquals(4, info.line.startLineIndex);
<a name="line153"></a>  assertEquals(7, info.line.endContentIndex);
<a name="line154"></a>  assertEquals(8, info.line.endLineIndex);
<a name="line155"></a>
<a name="line156"></a>  assertEquals(1, info.lineIndex);
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>function testGetLineDebugString() {
<a name="line160"></a>  var str = &#39;abcdefghijklmnop&#39;;
<a name="line161"></a>  var index = str.indexOf(&#39;j&#39;);
<a name="line162"></a>  var column = index + 1;
<a name="line163"></a>  assertEquals(
<a name="line164"></a>      goog.labs.format.csv.ParseError.getLineDebugString_(str, column),
<a name="line165"></a>      &#39;abcdefghijklmnop\n&#39; +
<a name="line166"></a>      &#39;         ^&#39;);
<a name="line167"></a>
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>function testIsCharacterString() {
<a name="line171"></a>  assertTrue(goog.labs.format.csv.isCharacterString_(&#39;a&#39;));
<a name="line172"></a>  assertTrue(goog.labs.format.csv.isCharacterString_(&#39;\n&#39;));
<a name="line173"></a>  assertTrue(goog.labs.format.csv.isCharacterString_(&#39; &#39;));
<a name="line174"></a>
<a name="line175"></a>  assertFalse(goog.labs.format.csv.isCharacterString_(null));
<a name="line176"></a>  assertFalse(goog.labs.format.csv.isCharacterString_(&#39;  &#39;));
<a name="line177"></a>  assertFalse(goog.labs.format.csv.isCharacterString_(&#39;&#39;));
<a name="line178"></a>  assertFalse(goog.labs.format.csv.isCharacterString_(&#39;aa&#39;));
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>function testAssertToken() {
<a name="line183"></a>  goog.labs.format.csv.assertToken_(&#39;a&#39;);
<a name="line184"></a>
<a name="line185"></a>  goog.object.forEach(goog.labs.format.csv.SENTINELS_,
<a name="line186"></a>      function(value) {
<a name="line187"></a>        goog.labs.format.csv.assertToken_(value);
<a name="line188"></a>      });
<a name="line189"></a>
<a name="line190"></a>  assertThrows(function() {
<a name="line191"></a>    goog.labs.format.csv.assertToken_(&#39;aa&#39;);
<a name="line192"></a>  });
<a name="line193"></a>
<a name="line194"></a>  assertThrows(function() {
<a name="line195"></a>    goog.labs.format.csv.assertToken_(&#39;&#39;);
<a name="line196"></a>  });
<a name="line197"></a>
<a name="line198"></a>  assertThrows(function() {
<a name="line199"></a>    goog.labs.format.csv.assertToken_({});
<a name="line200"></a>  });
<a name="line201"></a>}
</pre>


</body>
</html>
